//Filename    : PCCAPIUtils.cs
//Part of     : PCCAPI Example codes
//Description : Helper utilities, error management
//Version     : 3.2
//
//This example is only to be used with PC Connectivity API version 3.2.
//Compability ("as is") with future versions is not quaranteed.
//
//Copyright (c) 2005-2007 Nokia Corporation.
//
//This material, including but not limited to documentation and any related
//computer programs, is protected by intellectual property rights of Nokia
//Corporation and/or its licensors.
//All rights are reserved. Reproducing, modifying, translating, or
//distributing any or all of this material requires the prior written consent
//of Nokia Corporation. Nokia Corporation retains the right to make changes
//to this material at any time without notice. A copyright license is hereby
//granted to download and print a copy of this material for personal use only.
//No other license to any other intellectual property rights is granted. The
//material is provided "as is" without warranty of any kind, either express or
//implied, including without limitation, any warranty of non-infringement,
//merchantability and fitness for a particular purpose. In no event shall
//Nokia Corporation be liable for any direct, indirect, special, incidental,
//or consequential loss or damages, including but not limited to, lost profits
//or revenue,loss of use, cost of substitute program, or loss of data or
//equipment arising out of the use or inability to use the material, even if
//Nokia Corporation has been advised of the likelihood of such damages occurring.
namespace PCCAPIUtils
{
    using CONADefinitions;
    using PCCSErrors;

    class PCCAPIUtils
    {
        //===================================================================
        // CONAError2String --  Returns error text for given CONA error code
        //
        //
        //===================================================================
        public static string CONAError2String(int errorCode)
        {
            string functionReturnValue = null;
            functionReturnValue = "";
            switch (errorCode)
            {
                case PCCSErrors.CONA_OK:
                    functionReturnValue = "CONA_OK: Succeeded.";
                    break;
                case PCCSErrors.CONA_OK_UPDATED_MEMORY_VALUES:
                    functionReturnValue = "CONA_OK_UPDATED_MEMORY_VALUES: Everything OK, given data is updated because (free, used and total) memory values are changed!";
                    break;
                case PCCSErrors.CONA_OK_UPDATED_MEMORY_AND_FILES:
                    functionReturnValue = "CONA_OK_UPDATED_MEMORY_AND_FILES: Everything OK, given data is updated because files and memory values are changed!";
                    break;
                case PCCSErrors.CONA_OK_UPDATED:
                    functionReturnValue = "CONA_OK_UPDATED: Everything OK, given data is updated, unknown reason.";
                    break;
                case PCCSErrors.CONA_OK_BUT_USER_ACTION_NEEDED:
                    functionReturnValue = "CONA_OK_BUT_USER_ACTION_NEEDED: Operation needs some user action on Device";
                    break;
                case PCCSErrors.CONA_WAIT_CONNECTION_IS_BUSY:
                    functionReturnValue = "CONA_WAIT_CONNECTION_IS_BUSY: Operation started ok but other application has reserved the connection";
                    break;

                // Common error codes:
                case PCCSErrors.ECONA_INIT_FAILED:
                    functionReturnValue = "ECONA_INIT_FAILED: DLL initialization failed.";
                    break;
                case PCCSErrors.ECONA_INIT_FAILED_COM_INTERFACE:
                    functionReturnValue = "ECONA_INIT_FAILED_COM_INTERFACE: Failed to get connection to system.";
                    break;
                case PCCSErrors.ECONA_NOT_INITIALIZED:
                    functionReturnValue = "ECONA_NOT_INITIALIZED: API is not initialized.";
                    break;
                case PCCSErrors.ECONA_UNSUPPORTED_API_VERSION:
                    functionReturnValue = "ECONA_UNSUPPORTED_API_VERSION: API version not supported.";
                    break;
                case PCCSErrors.ECONA_NOT_SUPPORTED_MANUFACTURER:
                    functionReturnValue = "ECONA_NOT_SUPPORTED_MANUFACTURER: Manufacturer is not supported.";
                    break;

                case PCCSErrors.ECONA_UNKNOWN_ERROR:
                    functionReturnValue = "ECONA_UNKNOWN_ERROR: Failed, unknown error.";
                    break;
                case PCCSErrors.ECONA_UNKNOWN_ERROR_DEVICE:
                    functionReturnValue = "ECONA_UNKNOWN_ERROR_DEVICE: Failed, unknown error from device.";
                    break;
                case PCCSErrors.ECONA_INVALID_POINTER:
                    functionReturnValue = "ECONA_INVALID_POINTER: Required pointer is invalid.";
                    break;
                case PCCSErrors.ECONA_INVALID_PARAMETER:
                    functionReturnValue = "ECONA_INVALID_PARAMETER: Invalid parameter value.";
                    break;
                case PCCSErrors.ECONA_INVALID_HANDLE:
                    functionReturnValue = "ECONA_INVALID_HANDLE: Invalid handle.";
                    break;
                case PCCSErrors.ECONA_NOT_ENOUGH_MEMORY:
                    functionReturnValue = "ECONA_NOT_ENOUGH_MEMORY: Memory allocation failed in PC.";
                    break;
                case PCCSErrors.ECONA_WRONG_THREAD:
                    functionReturnValue = "ECONA_WRONG_THREAD: Failed, called interface was marshalled for a different thread.";
                    break;
                case PCCSErrors.ECONA_REGISTER_ALREADY_DONE:
                    functionReturnValue = "ECONA_REGISTER_ALREADY_DONE: Failed, notification interface is already registered.";
                    break;

                case PCCSErrors.ECONA_CANCELLED:
                    functionReturnValue = "ECONA_CANCELLED: Operation cancelled by ConnectivityAPI-User.";
                    break;
                case PCCSErrors.ECONA_NOTHING_TO_CANCEL:
                    functionReturnValue = "ECONA_NOTHING_TO_CANCEL: No running functions.";
                    break;
                case PCCSErrors.ECONA_FAILED_TIMEOUT:
                    functionReturnValue = "ECONA_FAILED_TIMEOUT: Operation failed because of timeout.";
                    break;
                case PCCSErrors.ECONA_NOT_SUPPORTED_DEVICE:
                    functionReturnValue = "ECONA_NOT_SUPPORTED_DEVICE: Device does not support operation.";
                    break;
                case PCCSErrors.ECONA_NOT_SUPPORTED_PC:
                    functionReturnValue = "ECONA_NOT_SUPPORTED_PC: Connectivity API does not support operation (not implemented).";
                    break;
                case PCCSErrors.ECONA_NOT_FOUND:
                    functionReturnValue = "ECONA_NOT_FOUND: Item was not found";
                    break;
                case PCCSErrors.ECONA_FAILED:
                    functionReturnValue = "ECONA_FAILED: The called operation failed.";
                    break;

                case PCCSErrors.ECONA_API_NOT_FOUND:
                    functionReturnValue = "ECONA_API_NOT_FOUND: Needed API module was not found from the system";
                    break;
                case PCCSErrors.ECONA_API_FUNCTION_NOT_FOUND:
                    functionReturnValue = "ECONA_API_FUNCTION_NOT_FOUND: Called API function was not found from the loaded API module";
                    break;

                // Device manager and device connection related errors:
                case PCCSErrors.ECONA_DEVICE_NOT_FOUND:
                    functionReturnValue = "ECONA_DEVICE_NOT_FOUND: Given phone is not connected (refresh device list).";
                    break;
                case PCCSErrors.ECONA_NO_CONNECTION_VIA_MEDIA:
                    functionReturnValue = "ECONA_NO_CONNECTION_VIA_MEDIA: Phone is connected but not via given media.";
                    break;
                case PCCSErrors.ECONA_NO_CONNECTION_VIA_DEVID:
                    functionReturnValue = "ECONA_NO_CONNECTION_VIA_DEVID: Phone is not connected with given DevID.";
                    break;
                case PCCSErrors.ECONA_INVALID_CONNECTION_TYPE:
                    functionReturnValue = "ECONA_INVALID_CONNECTION_TYPE: Connection type was invalid.";
                    break;
                case PCCSErrors.ECONA_NOT_SUPPORTED_CONNECTION_TYPE:
                    functionReturnValue = "ECONA_NOT_SUPPORTED_CONNECTION_TYPE: Device does not support connection type.";
                    break;
                case PCCSErrors.ECONA_CONNECTION_BUSY:
                    functionReturnValue = "ECONA_CONNECTION_BUSY: Other application has reserved connection.";
                    break;
                case PCCSErrors.ECONA_CONNECTION_LOST:
                    functionReturnValue = "ECONA_CONNECTION_LOST: Connection lost to device.";
                    break;
                case PCCSErrors.ECONA_CONNECTION_REMOVED:
                    functionReturnValue = "ECONA_CONNECTION_REMOVED: Connection removed, other application has reserved connection.";
                    break;
                case PCCSErrors.ECONA_CONNECTION_FAILED:
                    functionReturnValue = "ECONA_CONNECTION_FAILED: Connection failed, unknown reason.";
                    break;
                case PCCSErrors.ECONA_SUSPEND:
                    functionReturnValue = "ECONA_SUSPEND: Connection removed, PC goes to standby state.";
                    break;
                case PCCSErrors.ECONA_NAME_ALREADY_EXISTS:
                    functionReturnValue = "ECONA_NAME_ALREADY_EXISTS: Friendly name already exists.";
                    break;
                case PCCSErrors.ECONA_MEDIA_IS_NOT_WORKING:
                    functionReturnValue = "ECONA_MEDIA_IS_NOT_WORKING: Target media is active but it is not working (e.g. BT hardware stopped or removed).";
                    break;
                case PCCSErrors.ECONA_CACHE_IS_NOT_AVAILABLE:
                    functionReturnValue = "ECONA_CACHE_IS_NOT_AVAILABLE: Cache is not available (CONASearchDevices).";
                    break;
                case PCCSErrors.ECONA_MEDIA_IS_NOT_ACTIVE:
                    functionReturnValue = "ECONA_MEDIA_IS_NOT_ACTIVE: Target media is busy (or not ready yet).";
                    break;
                case PCCSErrors.ECONA_PORT_OPEN_FAILED:
                    functionReturnValue = "ECONA_PORT_OPEN_FAILED: Cannot open the changed COM port.";
                    break;

                // Device pairing releated errors:
                case PCCSErrors.ECONA_DEVICE_PAIRING_FAILED:
                    functionReturnValue = "ECONA_DEVICE_PAIRING_FAILED: Pairing failed.";
                    break;
                case PCCSErrors.ECONA_DEVICE_PASSWORD_WRONG:
                    functionReturnValue = "ECONA_DEVICE_PASSWORD_WRONG: Wrong password on device.";
                    break;
                case PCCSErrors.ECONA_DEVICE_PASSWORD_INVALID:
                    functionReturnValue = "ECONA_DEVICE_PASSWORD_INVALID: Password includes invalid characters or is missing.";
                    break;

                // File System errors:
                case PCCSErrors.ECONA_ALL_LISTED:
                    functionReturnValue = "ECONA_ALL_LISTED: All items are listed.";
                    break;
                case PCCSErrors.ECONA_MEMORY_FULL:
                    functionReturnValue = "ECONA_MEMORY_FULL: Device memory full.";
                    break;

                // File System errors for file functions:
                case PCCSErrors.ECONA_FILE_NAME_INVALID:
                    functionReturnValue = "ECONA_FILE_NAME_INVALID: File name contains invalid characters in Device or PC.";
                    break;
                case PCCSErrors.ECONA_FILE_NAME_TOO_LONG:
                    functionReturnValue = "ECONA_FILE_NAME_TOO_LONG: File name contains too many characters in Device or PC.";
                    break;
                case PCCSErrors.ECONA_FILE_ALREADY_EXIST:
                    functionReturnValue = "ECONA_FILE_ALREADY_EXIST: File already exists in Device or PC.";
                    break;
                case PCCSErrors.ECONA_FILE_NOT_FOUND:
                    functionReturnValue = "ECONA_FILE_NOT_FOUND: File does not exist in Device or PC.";
                    break;
                case PCCSErrors.ECONA_FILE_NO_PERMISSION:
                    functionReturnValue = "ECONA_FILE_NO_PERMISSION: Not allowed to perform required operation to file in device or PC.";
                    break;
                case PCCSErrors.ECONA_FILE_COPYRIGHT_PROTECTED:
                    functionReturnValue = "ECONA_FILE_COPYRIGHT_PROTECTED: Not allowed to perform required operation to file in device or PC.";
                    break;
                case PCCSErrors.ECONA_FILE_BUSY:
                    functionReturnValue = "ECONA_FILE_BUSY: Other application has reserved file in device or PC.";
                    break;
                case PCCSErrors.ECONA_FILE_TOO_BIG_DEVICE:
                    functionReturnValue = "ECONA_FILE_TOO_BIG_DEVICE: Device rejected the operation because file size is too big.";
                    break;
                case PCCSErrors.ECONA_FILE_TYPE_NOT_SUPPORTED:
                    functionReturnValue = "ECONA_FILE_TYPE_NOT_SUPPORTED: Device rejected the operation because file has unsupported type.";
                    break;
                case PCCSErrors.ECONA_FILE_NO_PERMISSION_ON_PC:
                    functionReturnValue = "ECONA_FILE_NO_PERMISSION_ON_PC: Not allowed to perform required operation to file in PC.";
                    break;
                case PCCSErrors.ECONA_FILE_EXIST:
                    functionReturnValue = "ECONA_FILE_EXIST: File move or rename: File is copied to target path with new name but removing the source file failed.";
                    break;
                case PCCSErrors.ECONA_FILE_CONTENT_NOT_FOUND:
                    functionReturnValue = "ECONA_FILE_CONTENT_NOT_FOUND: Specified file content does not found (e.g. unknown file section or stored position)";
                    break;
                case PCCSErrors.ECONA_FILE_OLD_FORMAT:
                    functionReturnValue = "ECONA_FILE_OLD_FORMAT: Specified file content supports old engine";
                    break;
                case PCCSErrors.ECONA_FILE_INVALID_DATA:
                    functionReturnValue = "ECONA_FILE_INVALID_DATA: Specified file data is invalid";
                    break;

                // File System errors for folder functions:
                case PCCSErrors.ECONA_INVALID_DATA_DEVICE:
                    functionReturnValue = "ECONA_INVALID_DATA_DEVICE: Device's folder contains invalid data.";
                    break;
                case PCCSErrors.ECONA_CURRENT_FOLDER_NOT_FOUND:
                    functionReturnValue = "ECONA_CURRENT_FOLDER_NOT_FOUND: Current folder is invalid in device (e.g MMC card removed).";
                    break;
                case PCCSErrors.ECONA_FOLDER_PATH_TOO_LONG:
                    functionReturnValue = "ECONA_FOLDER_PATH_TOO_LONG: Maximum folder path length is 255 characters.";
                    break;
                case PCCSErrors.ECONA_FOLDER_NAME_INVALID:
                    functionReturnValue = "ECONA_FOLDER_NAME_INVALID: Folder name includes invalid characters in Device or PC.";
                    break;
                case PCCSErrors.ECONA_FOLDER_ALREADY_EXIST:
                    functionReturnValue = "ECONA_FOLDER_ALREADY_EXIST: Folder already exists in target folder.";
                    break;
                case PCCSErrors.ECONA_FOLDER_NOT_FOUND:
                    functionReturnValue = "ECONA_FOLDER_NOT_FOUND: Folder does not exist in target folder.";
                    break;
                case PCCSErrors.ECONA_FOLDER_NO_PERMISSION:
                    functionReturnValue = "ECONA_FOLDER_NO_PERMISSION: Not allowed to perform required operation to folder in Device.";
                    break;
                case PCCSErrors.ECONA_FOLDER_NOT_EMPTY:
                    functionReturnValue = "ECONA_FOLDER_NOT_EMPTY: Not allowed to perform required operation because folder is not empty.";
                    break;
                case PCCSErrors.ECONA_FOLDER_NO_PERMISSION_ON_PC:
                    functionReturnValue = "ECONA_FOLDER_NO_PERMISSION_ON_PC: Not allowed to perform required operation to folder in PC.";
                    break;

                // Application installation error:
                case PCCSErrors.ECONA_DEVICE_INSTALLER_BUSY:
                    functionReturnValue = "ECONA_DEVICE_INSTALLER_BUSY: Cannot start device's installer.";
                    break;

                // Syncronization specific error codes:
                case PCCSErrors.ECONA_UI_NOT_IDLE_DEVICE:
                    functionReturnValue = "ECONA_UI_NOT_IDLE_DEVICE: Device rejects the operation. Maybe device's UI is not in idle state.";
                    break;
                case PCCSErrors.ECONA_SYNC_CLIENT_BUSY_DEVICE:
                    functionReturnValue = "ECONA_SYNC_CLIENT_BUSY_DEVICE: Device's SA sync client is busy.";
                    break;
                case PCCSErrors.ECONA_UNAUTHORIZED_DEVICE:
                    functionReturnValue = "ECONA_UNAUTHORIZED_DEVICE: Device rejects the operation. No permission.";
                    break;
                case PCCSErrors.ECONA_DATABASE_LOCKED_DEVICE:
                    functionReturnValue = "ECONA_DATABASE_LOCKED_DEVICE: Device rejects the operation. Device is locked.";
                    break;
                case PCCSErrors.ECONA_SETTINGS_NOT_OK_DEVICE:
                    functionReturnValue = "ECONA_SETTINGS_NOT_OK_DEVICE: Device rejects the operation. Maybe settings in Sync profile are wrong on Device.";
                    break;
                case PCCSErrors.ECONA_SYNC_ITEM_TOO_BIG:
                    functionReturnValue = "ECONA_SYNC_ITEM_TOO_BIG: Device rejected the operation";
                    break;
                case PCCSErrors.ECONA_SYNC_ITEM_REJECT:
                    functionReturnValue = "ECONA_SYNC_ITEM_REJECT: Device rejected the operation";
                    break;
                case PCCSErrors.ECONA_SYNC_INSTALL_PLUGIN_FIRST:
                    functionReturnValue = "ECONA_SYNC_INSTALL_PLUGIN_FIRST: Device rejected the operation";
                    break;

                // Versit conversion specific error codes :			
                case PCCSErrors.ECONA_VERSIT_INVALID_PARAM:
                    functionReturnValue = "ECONA_VERSIT_INVALID_PARAM: Invalid parameters passed to versit converter.";
                    break;
                case PCCSErrors.ECONA_VERSIT_UNKNOWN_TYPE:
                    functionReturnValue = "ECONA_VERSIT_UNKNOWN_TYPE: Failed, trying to convert versit formats not supported in VersitConverter.";
                    break;
                case PCCSErrors.ECONA_VERSIT_INVALID_VERSIT_OBJECT:
                    functionReturnValue = "ECONA_VERSIT_INVALID_VERSIT_OBJECT: Failed, validation of versit data not passed, contains invalid data.";
                    break;

                // Database specific error codes:
                case PCCSErrors.ECONA_DB_TRANSACTION_ALREADY_STARTED:
                    functionReturnValue = "Another transaction is already in progress";
                    break;
                case PCCSErrors.ECONA_DB_TRANSACTION_FAILED:
                    functionReturnValue = "Some of operations within a transaction failed and transaction was rolled back";
                    break;

                // Backup specific error codes:
                case PCCSErrors.ECONA_DEVICE_BATTERY_LEVEL_TOO_LOW:
                    functionReturnValue = "Failed, device rejects the restore operation. Device's battery level is low.";
                    break;
                case PCCSErrors.ECONA_DEVICE_BUSY:
                    functionReturnValue = "Failed, device rejects the backup/restore operation. Device's backup server busy.";
                    break;

                default:
                    functionReturnValue = "Undefined error code"; // shouldn't occur
                    break;
            }
            return functionReturnValue;
        }

        //===================================================================
        // ErrorMessageDlg --  Show an errormessage
        //
        //
        //===================================================================
        public static void ShowErrorMessage(string strError, int errorCode)
        {
            string strMessage = strError + "\n" + "\n" + string.Format("Error: {0:x2}", errorCode) + "\n" + CONAError2String(errorCode);
            System.Windows.Forms.MessageBox.Show(strMessage);
        }
    }
}